package com.gzy.rpsm_admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.rpsm_admin.mapper.BuildingMapper;
import com.gzy.rpsm_admin.pojo.Building;
import com.gzy.rpsm_admin.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// @Service与@Mapper对应，@Service表明这是一个专注于业务逻辑的类，是服务层组件
// 告诉Spring为我创建实例对象
@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired// spring为我自动创建BuildingMapper的实现类对象，并赋值给buildingMapper变量
    BuildingMapper buildingMapper;
    // BuildingMapper buildingMapper = new BuildingMapperImpl();

    //使用pageHelper分页查询building列表
    @Override
    public PageResult<Building> pageOfBuilding(Integer pageNum, Integer pageSize, String number) {
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据，这里并不会按BuildingMapper.xml中的sql查询所有数据
        // 而是根据PageHelper.startPage(pageNum, pageSize)中设置的参数
        // 计算出sql查询需要的LIMIT参数[offset=(pageNum-1)*pageSize, count=pageSize],
        // 拦截sql语句，动态生成一个新的sql语句, select * from building limit offset, count查询出列表数据
        // 同时会执行select count(1) from building 查询出总记录数
        Page<Building> buildings = buildingMapper.findAllBuilding(number);
        System.out.println("buildings length=" + buildings.size());
        // 将结果封装成PageResult对象返回，里面包含了总记录数total和列表数据list
        return PageResult.restPage(buildings);
    }
}